//
//  HeaderCollectionReusableView.m
//  14-HomeWork
//
//  Created by dllo on 16/8/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HeaderCollectionReusableView.h"
#import "HeaderCollectionViewCell.h"
#import "LabelAndButtonCollectionReusableView.h"
#define kHaderCollectionViewCell @"haderViewCell"
#define kLabelAndButtonReusableView @"headerForLabelAndButtonReuse"
@interface HeaderCollectionReusableView ()<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)UICollectionView *collectionView;
@end
@implementation HeaderCollectionReusableView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
       
               UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        
        CGFloat width = (CGRectGetWidth(frame) - 30) * 3 / 4;
        CGFloat height = (CGRectGetHeight(frame) - 40) / 3;
        flow.itemSize = CGSizeMake(width, height);
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flow.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        flow.minimumLineSpacing = 10;
        flow.minimumInteritemSpacing = 5;
//        flow.headerReferenceSize = CGSizeMake(0, 50);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 10, CGRectGetWidth(frame), CGRectGetHeight(frame) - 10) collectionViewLayout:flow];
        [self addSubview:_collectionView];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
//        inset
        _collectionView.contentInset = UIEdgeInsetsMake(30, 0, 0, 0);
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, -30, CGRectGetWidth(frame), 30)];
        lable.userInteractionEnabled = YES;
        lable.backgroundColor = [UIColor yellowColor];
        [self addSubview:lable];
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame) / 2, 30)];
        title.text = @"栏目";
        title.textColor = [UIColor colorWithRed:0.64 green:0.64 blue:0.64 alpha:1.00];
        title.backgroundColor = [UIColor blueColor];
        [lable addSubview:title];
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(frame) / 2, 0, CGRectGetWidth(frame) / 2, 30)];
        
        [button setTitleColor:[UIColor colorWithRed:0.64 green:0.64 blue:0.64 alpha:1.00] forState:UIControlStateNormal];
        
        [button setTitle:@"查看全部>" forState:UIControlStateNormal];
        
        [lable addSubview:button];
    // cell
        [_collectionView registerClass:[HeaderCollectionViewCell class] forCellWithReuseIdentifier:kHaderCollectionViewCell];
   // headerView
        [_collectionView registerClass:[LabelAndButtonCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kLabelAndButtonReusableView];
    }
    return self;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(0, 50);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 20;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHaderCollectionViewCell forIndexPath:indexPath];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    LabelAndButtonCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kLabelAndButtonReusableView forIndexPath:indexPath];
    reusableView.backgroundColor = [UIColor redColor];
    reusableView.label.text = @"栏目";
    return reusableView;
}
@end
